package com.dkm.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Department;
import com.dkm.entity.Role;
import com.dkm.entity.Users;
import com.dkm.service.DepartmentService;
import com.dkm.service.UsersService;
import com.dkm.util.LayuiJsonUilt;
import com.dkm.util.ResultUtil;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UsersService usersService;
	//注入部门业务层
	@Autowired
	private DepartmentService departmentService;
	
	//根据用户id获取用户信息
	@GetMapping("/findUserById")
	public Users findUserById(String user_id) {
		
		return usersService.findUsersById(user_id);
	}
	//根据用户id修改密码
	@PostMapping("/updatePwd")
	public ResultUtil updatePwd(String user_id,String oldPwd,String newPwd) {
		
		return usersService.updatePwd(user_id, oldPwd, newPwd);
	}
	//多条件分页查询用户
	@GetMapping
	@RequiresPermissions(value = "user:query")
	public LayuiJsonUilt<Users> findAllUsers(Users user,LayuiJsonUilt<Users> lj,String beginDate,String endDate){
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("user", user);
		if(beginDate!=null&&beginDate!="") {
			map.put("beginDate", beginDate);
		}
		if(endDate!=null&&endDate!="") {
			map.put("endDate", endDate);
		}
		
		//获取jpa分页对象
		Pageable pageable=PageRequest.of(lj.getPage(), lj.getLimit());
		
		Page<Users> page=usersService.findAllUSers(map, pageable);
		
		//设置数据
		lj.setData(page.getContent());
		//设置数据总条数
		lj.setCount(page.getTotalElements());
		
		return lj;
	}
	
	//新增用户
	@PostMapping
	@RequiresPermissions(value = "user:add")
	public ResultUtil addUser(Users user,String department_id) {
		//跟据部门id获取部门信息
		Department department=departmentService.findById(department_id).get();
		//设置给用户
		user.setDepartment(department);
		return usersService.saveAndFlushUser(user);
	}
	
	//查询用户名是否存在
	@GetMapping("/exists_login_name")
	public ResultUtil exists_login_name(String login_name) {
		Users user=usersService.findByLogin_name(login_name);
		if(user!=null) {
			return new ResultUtil(-1, "用户名已存在", null);
		}
		return new ResultUtil(0, "用户名不存在", null);
	}
	//修改用户
	@PutMapping
	@RequiresPermissions(value = "user:update")
	public ResultUtil updateUser(Users user,String department_id) {
		//跟据部门id获取部门信息
		Department department=departmentService.findById(department_id).get();
		//设置给用户
		user.setDepartment(department);
		return usersService.saveAndFlushUser(user);
	}
	
	//删除用户
	@DeleteMapping
	@RequiresPermissions(value = "user:delete")
	public ResultUtil deleteUser(String current_user_id,String user_id) {
		
		if(current_user_id.equals(user_id)) {
			return new ResultUtil(-1, "无法删除自己！", null);
		}
		return usersService.deleteUser(user_id);
	}
	//重置用户密码
	@GetMapping("/resetPwd")
	@RequiresPermissions(value = "user:resetPwd")
	public ResultUtil resetPwd(String user_id) {
		
		return usersService.resetPwd(user_id);
	}
	//根据用户id获取用户的角色集合
	@GetMapping("/getRolesByUser_id")
	public List<Role> getRolesByUser_id(String user_id) {
		return usersService.getRolesByUser_id(user_id);
	}
	
	//给用户设置角色
	@GetMapping("/set_roles")
	@RequiresPermissions(value = "user:setRoles")
	public ResultUtil setRoles(String user_id,HttpServletRequest request) {
		//获取角色id集合
		String[] roleIds=request.getParameterValues("roleIds[]");
		//给用户设置角色
		return usersService.setRoles(user_id, roleIds);
	}
	
	//锁定解锁用户
	@GetMapping("/locktopic")
	@RequiresPermissions(value = "user:lockAndUnlock")
	public ResultUtil locktopic(String current_user_id,String user_id,String is_lockout) {
		if(user_id.equals(current_user_id)) {
			return new ResultUtil(-1, "无法锁定自己！", null);
		}
		return usersService.locktopic(user_id, is_lockout);
	}
	
	//根据用户id获取部门信息
	@GetMapping("/getDepartmentByUser_id")
	public ResultUtil getDepartmentByUser_id(String current_user_id) {
		
		return usersService.getDepartmentByUser_id(current_user_id);
	}
	//用户签到
	@GetMapping("/sign_in")
	public ResultUtil signIn(String current_user_id) {
		return usersService.signIn(current_user_id);
	}
	//用户签退
	@GetMapping("/sign_out")
	public ResultUtil signOut(String current_user_id) {
		
		return usersService.signOut(current_user_id);
	}
	//修改用户头像信息
	@PostMapping("/updateUserHead_portrait")
	public ResultUtil updateUserHead_portrait(String user_id,String head_portrait) {
		
		return usersService.updateUserHead_portrait(user_id, head_portrait);
	}
	
	@GetMapping("/getAllUsers")
	public List<Users> getAllUsers() {
		return usersService.getAllUsers();
	}
}
